const baseUrl = 'http://60.173.236.227:2089/api';
var btns = document.querySelectorAll('.sq_btn');
var modal = document.querySelector('.dialog_modal');
var closeBtn = document.querySelector('.close_btn');
var btn = document.querySelector('.sus_btn');
var tabs = document.querySelectorAll('.cu_tab_item');
var sub_btn = document.querySelector('.dialog_btn');
var sqs_btn = document.querySelector('.sqs_btn');

//弹框打开
btn.addEventListener('click', function () {
    modal.style.display = 'block';
})

btns.forEach(function (btn) {
    btn.addEventListener('click', function () {
        modal.style.display = 'block';
    })
})
//弹框关闭
closeBtn.addEventListener('click', function () {
    modal.style.display = 'none';
    document.getElementById('name').value = ''
    document.getElementById('phone').value = ''
    document.getElementById('remark').value = ''
    document.getElementById('company_name').value = ''
})

//选项卡
tabs.forEach(function (tab) {
    tab.addEventListener('click', function () {
        modal.style.display = 'block';
    })
})

//dialog提交
sub_btn.addEventListener('click', function () {
    const name = document.getElementById('name').value;
    const phone = document.getElementById('phone').value;
    const remark = document.getElementById('remark').value;
    const company_name = document.getElementById('company_name').value;
    // console.log(name, phone, remark, company_name);
    if (name == '' || phone == '' || company_name == '') {
        alert('请填写完整信息');
        return false;
    }
    let data = {
        name: name,
        phone: phone,
        remark: remark,
        company_name: company_name
    }
    // console.log(123);
    let url = baseUrl + '/web/add_contacts';
    console.log(url);
    $.ajax({
        url:url,
        type:'post',
        dataType: 'jsonp', //=> 执行jsonp请求
        data: data,
        async:false,
        // jsonp: 'callback',
        success: (res) => {
            console.log('ok',res);
            if(res.code == 1){
                alert('提交成功');
                modal.style.display = 'none';
            }else{
                alert(res.msg)
            }
        }
    })

})

//页面提交

sqs_btn.addEventListener('click', function () {
    const name = document.getElementById('y_name').value;
    const phone = document.getElementById('y_phone').value;
    const remark = document.getElementById('y_remark').value; 
    const company_name = document.getElementById('y_company_name').value;
    if (name == '' || phone == '' || company_name == '') {
        alert('请填写完整信息');
        return false;
    }
    let data = {
        name: name,
        phone: phone,
        remark: remark,
        company_name: company_name
    }
    let url = baseUrl + '/web/add_contacts';
    $.ajax({
        url:url,
        type:'post',
        dataType: 'jsonp',
        data: data,
        async:false,
        success: (res) => {
            if(res.code == 1){
                alert('提交成功');
            }else{
                alert(res.msg)
            }
        }
    })
})
